package com.zhou.framework.model;

import lombok.EqualsAndHashCode;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author lang.zhou
 * @date 2022/4/28 13:32
 */
@EqualsAndHashCode(callSuper = true)
public class RS extends HashMap<String,Object> {
    public RS() {
        put("msg","");
        put("code",200);
        put("data",new HashMap<>());
    }

    public RS(String msg, int code) {
        put("msg",msg);
        put("code",code);
    }

    public RS put(String k, Object v){
        super.put(k,v);
        return this;
    }

    public boolean isSuccess(){
        return Objects.equals(200,get("code"));
    }

    @SuppressWarnings("all")
    public RS set(String k, Object v){
        Object data = this.computeIfAbsent("data",m->new HashMap<>(1));
        Map<String,Object> map = null;
        if(data instanceof Map){
            map = (Map<String, Object>) data;
        }
        if(map != null){
            map.put(k,v);
        }
        return this;
    }
    public RS putData(String k, Object v){

        return set(k,v);
    }

    public static RS failed(String message){
        return failed(message,500);
    }
    public static RS failed(String message, Integer code){
        return new RS(message,code);
    }
    public static RS success(String message){
        return new RS(message,200);
    }

    public static RS data(Object data){
        return RS.success().put("data",data);
    }

    public static RS mapData(){
        return RS.data(new HashMap<>());
    }

    public static RS success(){
        return new RS("",200);
    }


}

